#include "dialog_dico.h"
#include "ui_dialog_dico.h"

Dialog_Dico::Dialog_Dico(QWidget *parent, LayerProcess* _pProcess) :
	QDialog(parent),
	ui(new Ui::Dialog_Dico)
{
	ui->setupUi(this);
	pProcess = _pProcess;
	isInitDone = false;
	
	ui->label_4->setVisible(false);
	ui->label_4->setText("");
	ui->label_5->setVisible(false);
	ui->label_5->setText("");
	
	ui->widgetAddDico->setVisible(false);
	
	/* Load last dico */
	QString lastDico = pProcess->LoadLastDico();
	/* Get dico list */
	pProcess->displayDicos(ui->comboBox);
	/* Set last dico from the list */
	ui->comboBox->setCurrentIndex( ui->comboBox->findText( lastDico ) );
	/* Display words from the dico */
	pProcess->displayAllWords(ui->tableWidget);
	
	isInitDone = true;
	ui->lineEdit->setFocus();
}

Dialog_Dico::~Dialog_Dico()
{
	delete ui;
}

void Dialog_Dico::on_pushButton_3_clicked()
{
	this->close();
	this->~Dialog_Dico();
}

void Dialog_Dico::on_pushButton_clicked()
{
    ui->pushButton->setDisabled(true);
	ui->label_5->setVisible(false);
	ui->label_4->setVisible(true);
	ui->label_4->setText("Word adding...");
	
	if( pProcess->AddWord(ui->lineEdit->text(), ui->lineEdit_2->text()) )
	{
		ui->label_4->setText("Word added \nsuccessfully!");
		pProcess->displayAllWords(ui->tableWidget);
	}
	else
		ui->label_4->setText("Error occured!");
		
	ui->pushButton->setDisabled(false);
	ui->lineEdit->clear();
	ui->lineEdit_2->clear();
	ui->lineEdit->setFocus();
	
}

void Dialog_Dico::on_pushButton_2_clicked()
{
	ui->pushButton_2->setDisabled(true);
	ui->label_4->setVisible(false);
	ui->label_5->setVisible(true);
	ui->label_5->setText("Removing word...");
	
	if( pProcess->DeleteWord(ui->tableWidget->currentRow()) )
	{
		ui->label_5->setText("Word removed \nsuccessfully!");
		pProcess->displayAllWords(ui->tableWidget);
	}
	else
		ui->label_5->setText("Error occured!");
	
	ui->pushButton_2->setDisabled(false);
	
}

void Dialog_Dico::on_comboBox_currentIndexChanged(const QString &arg1)
{
	if(isInitDone)
	{
		if(arg1 == "+ Add Dictionary")
		{
			ui->widgetAddDico->setEnabled(true);
			ui->widgetAddDico->setVisible(true);
			ui->widgetModifDico->setEnabled(false);
			
			ui->tableWidget->clearContents();
			pProcess->ClearDico();
			
			ui->lineEdit_3->setFocus();
		}
		else
		{
			ui->widgetAddDico->setEnabled(false);
			ui->widgetAddDico->setVisible(false);
			ui->widgetModifDico->setEnabled(true);
			
			pProcess->LoadDico(arg1);
			pProcess->displayAllWords(ui->tableWidget);
			
			ui->lineEdit->setFocus();
			ui->pushButton->setAutoDefault(true);
		}
	}
	
}

void Dialog_Dico::on_pushButton_4_clicked()
{
	ui->pushButton_4->setEnabled(false);
	
    pProcess->CreateDico(ui->lineEdit_3->text());
	pProcess->displayDicos(ui->comboBox);
	ui->comboBox->setCurrentIndex( ui->comboBox->findText( ui->lineEdit_3->text() ) );
	
	ui->lineEdit_3->clear();
	
	ui->pushButton_4->setEnabled(true);
}



